package com.kd.juhaowan.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallbackProxy;
import com.kd.juhaowan.R;
import com.kd.juhaowan.http.api.CommentDetailApi;
import com.kd.juhaowan.http.api.DianZanApi;
import com.kd.juhaowan.http.model.CommentDetailModel;
import com.kd.juhaowan.http.model.CommentModel;
import com.kd.juhaowan.http.model.FollowResult;
import com.kd.juhaowan.http.model.HttpData;
import com.kd.juhaowan.http.model.NewsPostModel;
import com.kd.juhaowan.http.model.SubCommentModel;
import com.kd.juhaowan.ui.adapter.BaseRecyclerViewAdapter;
import com.kd.juhaowan.ui.adapter.BaseViewHolder;
import com.kd.juhaowan.ui.holder.CommentDetailContentViewHolder;
import com.kd.juhaowan.ui.holder.CommentDetailHeadViewHolder;
import com.kd.juhaowan.ui.listener.OnItemClickListener;
import com.kd.juhaowan.ui.listener.OnItemDianzanListener;
import com.kd.juhaowan.ui.listener.OnItemPinglunListener;
import com.kd.juhaowan.utils.LogUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class CommentDetailActivity extends BaseRefreshActivity implements OnItemClickListener, OnItemDianzanListener, OnItemPinglunListener {

    @BindView(R.id.view_refreshlist_refresh)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.view_refreshlist_list)
    RecyclerView mRecyclerView;
    @BindView(R.id.newsdetail_comment)
    TextView tipText;



    @BindView(R.id.activity_title)
    TextView titleView;

    @Override
    protected void reInitDate() {
        onRefresh(getSmartRefreshLayout());
    }

    private CommentModel model;

    @Override
    protected SmartRefreshLayout getSmartRefreshLayout() {
        return refreshLayout;
    }

    @Override
    protected String getSmartRefreshTaskTag() {
        return null;
    }
    private BaseRecyclerViewAdapter<BaseViewHolder, Object> mAdapter;
    @Override
    protected void onRequestNextPageData(int page) {
        EasyHttp.post(this).api(new CommentDetailApi(model.getGame_id(),page,model.getId())).request(new HttpCallbackProxy<HttpData<CommentDetailModel>>(this){
            @Override
            public void onHttpSuccess(HttpData<CommentDetailModel> result, boolean cache) {
                super.onHttpSuccess(result, cache);
                if(page==1){
                    tipText.setText("回复@"+result.getData().getOne_comment().getUser_text().getNickname());
                    List<Object> list = new ArrayList<>();
                    list.add(result.getData().getOne_comment());
                    list.addAll(result.getData().getList());
                    mAdapter.resetAdapterData(list);
                }else{
                    List<Object> list = new ArrayList<>();
                    list.addAll(result.getData().getList());
                    mAdapter.appendAdapterData(list);
                }

            }
        });
    }

    @Override
    protected boolean isNullData(HttpData response) {
        return false;
    }



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        titleView.setText("全部回复");
        model = (CommentModel)getIntent().getSerializableExtra("model");
//        setContentView(R.layout.activity_account_login);
        setAdapter();
        onRefresh(getSmartRefreshLayout());
        register = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                LogUtil.debugLog( "onActivityResult: _____" + result);
                if (result != null) {
                    Intent intent = result.getData();
                    if (result.getResultCode() == Activity.RESULT_OK) {
//                        if(replyModel!=null&& TextUtils.isEmpty(replyModel.getReply_to_reply_id())){
//
//                        }

                        onRefresh(getSmartRefreshLayout());
                    }
                }
            }
        });

    }


    private void setAdapter(){
        mAdapter = new BaseRecyclerViewAdapter<BaseViewHolder, Object>() {
            @NonNull
            @Override
            public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                BaseViewHolder holder;
                if(ITEM_VIEW_TYPE_Title==viewType){
                    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
                    View view = inflater.inflate(R.layout.item_commentdetail_head, parent, false);
                    holder = new CommentDetailHeadViewHolder(view);
                    holder.setOnItemClickListener(getOnItemClickListener());
                }else {
                    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
                    View view = inflater.inflate(R.layout.item_commentdetail_content, parent, false);
                    holder = new CommentDetailContentViewHolder(view);
                    holder.setOnItemClickListener(getOnItemClickListener());
                }
                return holder;
            }

            @Override
            public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
                if(holder instanceof CommentDetailHeadViewHolder){
                    ((CommentDetailHeadViewHolder) holder).setDate((CommentModel) getItem(position));
                }else if (holder instanceof CommentDetailContentViewHolder){
                    ((CommentDetailContentViewHolder) holder).setDate((SubCommentModel) getItem(position),position==mAdapter.getItemCount()-1);
                }
            }

            @Override
            public int getItemViewType(int position) {
                if(position==0){
                    return ITEM_VIEW_TYPE_Title;
                }else {
                    return ITEM_VIEW_TYPE_Info;
                }
            }
        };

        mAdapter.setOnItemClickListener(this);
        mAdapter.resetAdapterData(null);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(RecyclerView.VERTICAL);

        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    public void onItemClick(RecyclerView.ViewHolder viewHolder, View view, int position) {

    }

    @Override
    public void onItemPinglunClick(BaseViewHolder viewHolder, int position) {
        SubCommentModel item = (SubCommentModel)mAdapter.getItem(position);
        Intent intent = new Intent(this, CommentCommentActivity.class);
        NewsPostModel replyModel =new NewsPostModel();
        replyModel.setGame_id(model.getGame_id());
        replyModel.setComment_id(model.getId());
        replyModel.setReply_to_reply_id(item.getId());
        intent.putExtra("model",replyModel);
        register.launch(intent);
    }

    @Override
    public void onItemDianzanClick(BaseViewHolder viewHolder, int position) {
        if(viewHolder instanceof  CommentDetailHeadViewHolder){
            CommentModel item = (CommentModel)mAdapter.getItem(position);
            EasyHttp.post(this).api(new DianZanApi(model.getGame_id(),item.getId())).request(new HttpCallbackProxy<HttpData<FollowResult>>(this){
                @Override
                public void onHttpSuccess(HttpData<FollowResult> result, boolean cache) {
                    super.onHttpSuccess(result, cache);
                    if(result.getData().getStatus()==1){
                        item.setLike_count(item.getLike_count()+1);
                        item.setIs_like(1);
                    }else {
                        item.setLike_count(item.getLike_count()-1);
                        item.setIs_like(0);
                    }
                    mAdapter.notifyItemChanged(position,item);
                    GameDetailActivity2.needRefresh =true;
                }
            });
        }else if (viewHolder instanceof  CommentDetailContentViewHolder){
            SubCommentModel item = (SubCommentModel)mAdapter.getItem(position);
            EasyHttp.post(this).api(new DianZanApi(model.getGame_id(),item.getId())).request(new HttpCallbackProxy<HttpData<FollowResult>>(this){
                @Override
                public void onHttpSuccess(HttpData<FollowResult> result, boolean cache) {
                    super.onHttpSuccess(result, cache);
                    if(result.getData().getStatus()==1){
                        item.setLike_count(item.getLike_count()+1);
                        item.setIs_like(1);
                    }else {
                        item.setLike_count(item.getLike_count()-1);
                        item.setIs_like(0);
                    }
                    mAdapter.notifyItemChanged(position,item);
                }
            });
        }
    }

    @OnClick(R.id.actvity_back)
    public void onBackClick(){
        finish();
    }
    @Override
    protected int getViewLayoutResourceId() {
        return R.layout.activity_comment_detail;
    }
    @OnClick(R.id.newsdetail_comment)
    public void onCommentClick(){
        Intent intent = new Intent(this, CommentCommentActivity.class);
        NewsPostModel replyModel =new NewsPostModel();
        replyModel.setGame_id(model.getGame_id());
        replyModel.setComment_id(model.getId());
        replyModel.setReply_to_reply_id("0");
        intent.putExtra("model",replyModel);
        register.launch(intent);

    }
    private ActivityResultLauncher<Intent> register;
}
